<template>
  <div class="adminList-wrap">
    <el-button-group
        style="margin-top: 10px; margin-bottom: 30px; float: right"
    >
      <el-button
          type="primary"
          style="margin-right: 20px"
          :icon="Plus"
          @click="fnRegister"
      >
        注册开发者
      </el-button>
    </el-button-group>
    <el-input
        v-model="searchInput"
        style="width: 340px; margin-bottom: 30px"
        size="large"
        placeholder="请输入搜索内容"
        :prefix-icon="Search"
        clearable
        @input="fnInput"
    />
    <el-table
        :data="datas"
        border
        style="width: 100%"
        :header-cell-style="{ background: '#f5f5f5', color: '#606266' }"
    >
      <!-- <el-table-column
          type="selection"
          width="55"
          align="center"
          fixed="left"
      /> -->
      <el-table-column label="开发者ID" min-width="220" align="center">
        <template #default="scope">
          <router-link class="router" :to="'/application?dev_id='+scope.row.dev_id">
            {{
              scope.row.dev_id ? scope.row.dev_id : '-'
            }}
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="邮箱" min-width="160" align="center">
        <template #default="scope">{{
            scope.row.email ? scope.row.email : '-'
          }}</template>
      </el-table-column>
      <el-table-column label="注册时间" min-width="160" align="center">
        <template #default="scope">{{
            scope.row.created_time ? scope.row.created_time : '-'
          }}</template>
      </el-table-column>
      <el-table-column label="状态" min-width="130" align="center">
        <template #default="scope">
          <span v-if="scope.row.status == 0">
            <el-button
                class="upload"
                size="small"
                type="success"
                @click="frozenFn(scope.$index, scope.row)"
            >冻结</el-button
            >
          </span>
          <span v-if="scope.row.status == -1">
            <el-button
                class="upload"
                size="small"
                type="warning"
                @click="activeFn(scope.$index, scope.row)"
            >激活</el-button
            >
          </span>
        </template>
      </el-table-column>
      <el-table-column
          label="操作"
          align="center"
          min-width="130"
          fixed="right"
      >
        <template #default="scope">
          <el-button type="danger" size="small" @click="fnDelete(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <el-pagination
        class="page-warp"
        background
        layout="total,prev, pager, next, sizes"
        v-model:page-size="pageSize"
        :total="total"
        :page-sizes="[10, 20, 30, 40]"
        :current-page="currentPage"
        @current-change="fnCurrentChange"
        @size-change="fnSizeChange"
    />
  </div>
</template>

<script setup>
import { reactive, ref, onMounted, watch } from 'vue'
import { useRouter } from 'vue-router'
import { postData } from '@/http/request.js'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus, Search } from '@element-plus/icons-vue'
let router = useRouter()

let datas = ref([])
let res_datas = ref([])
let total = ref(0)
let currentPage = ref(1)
let pageSize = ref(10)
let searchInput = ref('') ////搜索

//init
let getList = () => {
  postData(
      '/dev/list',
      {},
      {
        timeout: 120000,
      }
  )
      .then((res) => {
        if (res.code == 0) {
          total.value = res?.datas?.developers?.length || 0
          res_datas.value = res?.datas?.developers || []
          datas.value = res_datas.value.slice(0, pageSize.value)
        } else if (res.code == -56055) {
          return
        } else {
          // ElMessage({
          //   type: 'error',
          //   message: 'error',
          // })
        }
      })
      .catch((res) => {
        ElMessage({
          type: 'error',
          message: res
        })
      })
}

//删除,当前行
let fnDelete = (dataRow) => {
  ElMessageBox.confirm('确认删除该开发者?', '删除确认', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
      .then(() => {
        postData('/dev/delete', {
          developers: [dataRow.dev_id],
        }).then((res) => {
          if (res.code == 0) {
            ElMessage({
              type: 'success',
              message: '删除成功',
            })
            currentPage.value = 1
            getList()
          } else {
            ElMessage({
              type: 'error',
              message: '删除失败',
            })
          }
        })
      })
      .catch(() => {})
}

//分页
let fnSizeChange = (data) => {
  pageSize.value = data
  currentPage.value = 1
  getList()
}
let fnCurrentChange = (data) => {
  let start = (data - 1) * pageSize.value
  currentPage.value = data
  datas.value = res_datas.value.slice(start, start + pageSize.value)
}

//search
let fnInput = (val) => {
  if (val) {
    datas.value = datas.value.filter((item) => {
      return Object.keys(item).some((key) => {
        return String(item[key]).toLowerCase().indexOf(val) > -1
      })
    })
  } else {
    getList()
  }
  currentPage.value = 1
  total.value = datas.value.length
}

let fnRegister = () => {
  router.push('/developerAdd')
}

//点击冻结按钮
let frozenFn = (index, row) => {
  postData('/dev/app/update/status', {
    dev_id: row.dev_id,
    status: -1,
  })
      .then((res) => {
        if (res.code == 0) {
          ElMessage.success('已激活')
          getList()
        } else {
          ElMessage.error(res.code + '，' + res.msg)
        }
      })
      .catch((res) => {
        ElMessage.error(res.code + '，' + res.msg)
      })
}

//点击激活按钮
let activeFn = (index, row) => {
  postData('/dev/app/update/status', {
    dev_id: row.dev_id,
    status: 0,
  })
      .then((res) => {
        if (res.code == 0) {
          ElMessage.success('已冻结')
          getList()
        } else {
          ElMessage.error(res.code + '，' + res.msg + 3)
        }
      })
      .catch((res) => {
        ElMessage.error(res.code + '，' + res.msg + 4)
      })
}

onMounted(() => {
  getList()
})
</script>

<style scoped>
.adminList-wrap {
  width: 100%;
  height: 100%;
  position: relative;
}
.search-wrap {
  display: flex;
  justify-content: flex-start;
}

.adminList-wrap >>> .el-table__header-wrapper {
  background: red;
}
.adminList-wrap >>> .el-table__header {
  background: red;
}
.page-warp {
  float: right;
  margin-top: 10px;
}
.router {
  color: rgb(49, 130, 189);
  text-decoration: none;
}
</style>
